package com.baomidou.springboot.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.springboot.ErrorCode;
import com.baomidou.springboot.entity.Product;
import com.baomidou.springboot.entity.dto.ProductDTO;
import com.baomidou.springboot.entity.vo.PostVO;
import com.baomidou.springboot.entity.vo.ProductVO;
import com.baomidou.springboot.mapper.ProductMapper;
import com.baomidou.springboot.service.ProductService;
import lombok.Data;
import lombok.experimental.Accessors;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/{id}")
    public ApiResult getProductById(@PathVariable("id") Long id) {
        Product product = productService.getById(id);
        if (Objects.isNull(product)) {
            return ApiResult.restResult(product, ErrorCode.TEST);
        }
        return ApiResult.restResult(product, ErrorCode.SUCCESS);
    }


    @Data
    @Accessors(chain = true)
    class ListVO {
        Long total;
        List<ProductVO> list;
    }

//    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
//    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    @GetMapping
    public ApiResult getProductList(ProductDTO dto) {
        LambdaQueryWrapper<Product> lambda = new QueryWrapper<Product>().lambda();
        if ( ! StringUtil.isBlank(dto.getTagName())) {
            lambda.eq(Product::getProductTag,dto.getTagName());
        }

        if ( ! StringUtil.isBlank(dto.getProductName())) {
            lambda.like(Product::getProductName,dto.getProductName());
        }

        lambda.orderByDesc(Product::getCreateAt);
        IPage<Product> page = productService.page(new Page<>(dto.getPageNum(), dto.getPageSize()), lambda);
        List<Product> records = page.getRecords();
        List<ProductVO> vo = new ArrayList<>();
        records.forEach((v)->{
            ProductVO productVO = new ProductVO().setId(String.valueOf(v.getId()));
            BeanUtils.copyProperties(v, productVO);
            vo.add(productVO);
        });

        return ApiResult.restResult(new ListVO().setTotal(page.getTotal()).setList(vo), ErrorCode.SUCCESS);
    }

    @PostMapping
    public ApiResult addProduct(@RequestBody Product product) {
        if (productService.save(product)){
            return ApiResult.restResult(product, ErrorCode.SUCCESS);
        }
        return ApiResult.restResult(product, ErrorCode.TEST);
    }

    @PutMapping("/{id}")
    public ApiResult updateProduct(@PathVariable("id") Long id, @RequestBody Product product) {
        product.setId(id);
        if (productService.updateById(product)) {
            return ApiResult.restResult(product, ErrorCode.SUCCESS);
        }
        return ApiResult.restResult(product, ErrorCode.TEST);
    }

    @DeleteMapping("/{id}")
    public ApiResult deleteProduct(@PathVariable("id") Long id) {
        if (productService.removeById(id)) {
            return ApiResult.restResult(id, ErrorCode.SUCCESS);
        }
        return ApiResult.restResult(id, ErrorCode.TEST);
    }


    @GetMapping("/tag")
    public ApiResult getTag() {
        return ApiResult.restResult(productMapper.getTagName(), ErrorCode.SUCCESS);
    }

}
